﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace NSATBPortal
{
    public partial class TestDataSetImport : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void buttonImport_Click(object sender, EventArgs e)
        {
            string sTargetFileURL = "";
            if (fileuploadXLS_Upload.HasFile)
            {
                string filename = Path.GetFileName(fileuploadXLS_Upload.FileName);
                if (Path.GetExtension(filename).ToLower() != ".xlsx")
                {
                    lblErrorMessage.Text = "ERROR: You must select a valid Test Data Set Excel Template (.xlsx).";
                }
                else
                {
                    try
                    {
                        sTargetFileURL = Server.MapPath("~/uploads/") + "{" + Guid.NewGuid().ToString() + "}" + filename;
                        fileuploadXLS_Upload.SaveAs(sTargetFileURL);
                        //lblErrorMessage.Text = "Upload status: File uploaded!";
                    }
                    catch (Exception ex)
                    {
                        lblErrorMessage.Text = "ERROR: The file could not be uploaded. The following error occured: " + ex.Message;
                        return;
                    }
                }
            }
            else
            {
                lblErrorMessage.Text = "ERROR: You must select a valid Test Data Set Excel Template (.xlsx).";
                return;
            }

            // Import Test Case
            string sUserID = Request.LogonUserIdentity.Name;
            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                lblErrorMessage.Text = objATBDB.ImportDefTestDataSet(sTargetFileURL, sUserID);
            }
        }

    }
}